package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
	@Reference
	PermissionService permissionService;

	@RequestMapping("/findByPage")
	public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
		log.debug("PermissionController{" + queryPageBean + "}");
		PageResult pageResult = permissionService.findByPage(queryPageBean);
		log.debug("权限分页查询成功");
		return new Result(true, MessageConst.QUERY_PERMISSION_SUCCESS, pageResult);
	}

	@RequestMapping("/add")
	public Result add(@RequestBody Permission permission) {
		log.debug("PermissionController:{" + permission + "}");
		permissionService.add(permission);
		log.debug("权限添加成功");
		return new Result(true, MessageConst.ADD_PERMISSION_SECCESS);
	}

	@RequestMapping("/findById")
	public Result findById(Integer id) {
		log.debug("controller -- findById: {" + id + "}");
		Permission permission = permissionService.findById(id);
		log.debug("权限根据id查询：{" + permission + "}");
		return new Result(true, MessageConst.QUERY_PERMISSION_SUCCESS, permission);
	}

	@RequestMapping("/edit")
	public Result edit(@RequestBody Permission permission) {
		log.debug("PermissionController--edit:" + permission + "}");
		permissionService.edit(permission);
		log.debug("权限编辑成功");
		return new Result(true, MessageConst.EDIT_PERMISSION_SUCCESS);
	}

	@RequestMapping("/delById")
	public Result delById(Integer id) {
		log.debug("PermissionController--delete;{" + id + "}");
		permissionService.delById(id);
		log.debug("权限删除成功");
		return new Result(true, MessageConst.DELETE_PERMISSION_SUCCESS);
	}
	//查询所有权限列表
	@RequestMapping("/findAll")
	//@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
	public Result findAll(){
		log.debug("permissionController-findAll");
		List<Permission> list = permissionService.findAll();//发送请求
		log.debug("PermissionList: { " + list + " }");
		return new Result(true, MessageConst.QUERY_PERMISSION_SUCCESS,list);
	}
}
